package com.ojoin.ojoin.eb_spider.service.catchdata.amazon;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.openqa.selenium.WebElement;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.service.ISLDataCatchAtomStepService;
import com.ojoin.ojoin.eb_spider.service.WebDriverService;
import com.ojoin.trade.common.utils.JavaMethodTimeOutUtils;


@Scope("prototype")
@Service
public class AMAZONLogin implements ISLDataCatchAtomStepService<String> {
 
	public WebDriverService webDriver;
	public String username;
	public String password;
	public boolean verificate = false ;
	public int  count = 0 ;

	@Override
	public void get() {
		if(count == 0){
			webDriver.get("https://vendorcentral.amazon.cn/gp/vendor/sign-in?ie=UTF8&originatingURI=%2Fst%2Fvendor%2Fmembers%2Fhome");
			
		}
		
	}

	@Override
	public void sendKeys() throws Exception {
		
		if (verification()) {
			webDriver.sendKeys(WebDriverService.ID, "ap_email", username);
			verificate = true ;
			webDriver.sendKeys("id", "ap_password", password);
			
			// 验证码处理
			WebElement verifyE = webDriver.findElement(WebDriverService.CSS,
					"#ap_captcha_img > img");
			 
			String code = webDriver.getVerifyCode(verifyE);
			 
			
			webDriver.sendKeys(WebDriverService.ID, "ap_captcha_guess", code);
			
			
			
		}else {
			webDriver.sendKeys(WebDriverService.ID, "username", username);
			webDriver.sendKeys(WebDriverService.ID, "password", password);
		}
	}

	public boolean  verification(){
		boolean isCome = false;

		try {
			Callable<String> call = new Callable<String>() {
				public String call() throws Exception {
					// 开始执行耗时操作
					webDriver.findElement("id","ap_captcha_table");
					return "线程执行完成.";
				}
			};

			JavaMethodTimeOutUtils.excute(call);

			isCome = true;
		} catch (Exception e) {

		}
		
		return isCome;
	}
	
	
	
	@Override
	public void submit() {
		if(verificate){
			webDriver.click("id", "signInSubmit-input");
		}else {
			webDriver.click("id", "login-button-container");
		}
count++;
	}

	@Override
	public List<String> result() {
		String text = webDriver
				.getText(
						WebDriverService.CSS,
						"#frmLogin > div > div > div.entry-tip.hide > table > tbody > tr > td:nth-child(2) > span");
		List<String> li = new ArrayList<String>();
		li.add(text);
		return li;

	}

	public boolean isUserAndPassword() {
		// TODO
		return false;
	}

	public boolean isVerifycodeError() {
		// TODO
		return false;
	}

	public void init(WebDriverService webDriver, String username, String password) {
		this.webDriver = webDriver;
		this.username = username;
		this.password = password;

	}

	@Override
	public void run() throws Exception {
		get();
		sendKeys();
		submit();
	}

	public boolean islogin() {
		//webDriver.get("https://vendorcentral.amazon.cn/gp/vendor/members/invoice/home?ref_=vc_ven-home_subNav");
		return webDriver
				.isLogin("https://vendorcentral.amazon.cn/st/vendor/members/home");
	}

	@Override
	public void fiter(List<String> list, Map<String, String> map) {
		// TODO Auto-generated method stub
		
	}

}
